package leetcode.interview1714;

import java.util.PriorityQueue;

public class Solution extends Interview1714 {

	@Override
	public int[] smallestK(int[] arr, int k) {
		if (k == 0)
			return new int[] {};
		if (arr == null || arr.length == 0 || arr.length < k)
			return arr;
		PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
		for (int element : arr) {
			if (pq.size() < k) {
				pq.add(element);
			} else if (element < pq.peek()) {
				pq.poll();
				pq.add(element);
			}
		}
		int[] result = new int[k];
		for (int i = 0; i < k; i++) {
			result[i] = pq.poll();
		}
		return result;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
	}

}
